<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/css/style.css" type="text/css" media="all"/>
<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" media="all"/>
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<!--<link href="--><?php //echo URL::base() ?><!--media/vendor/fixtable/css/component.css" rel="stylesheet">-->
<!--<link href="--><?php //echo URL::base() ?><!--media/vendor/fixtable/css/normalize.css" rel="stylesheet">-->
<!--<link href="--><?php //echo URL::base() ?><!--media/vendor/css/jquery-ui-timepicker-addon.min.css" rel="stylesheet">-->
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<!--<script src="--><?php //echo URL::base() ?><!--media/vendor/js/jquery-ui-1.10.4.custom.js"></script>-->
<!--<script src="--><?php //echo URL::base() ?><!--media/vendor/js/jquery-ui-timepicker-addon.min.js"></script>-->
<!--<script src="--><?php //echo URL::base() ?><!--media/vendor/js/jquery-ui-timepicker-zh-CN.js"></script>-->
<!--<script src="--><?php //echo URL::base() ?><!--media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>-->
<script src="<?php echo URL::base() ?>media/vendor/js/Validform_v5.3.2_ncr_min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.printPage.js"></script>
<script src="<?php echo URL::base() ?>media/js/jquery.form.js"></script>
<script src="<?php echo URL::base() ?>media/laydate/laydate.js"></script>
<style>
    .table td, .table th {
        text-align: center;
    }
    /*.form-horizontal .control-label {*/
        /*width: 100px;*/
    /*}*/

    /*.form-horizontal .controls {*/
        /*margin-left: 110px;*/
    /*}*/

    a.xiazai:hover {
        text-decoration: none;
    }

    table tbody tr:nth-child(2n) {
        background-color: #f5f5f5;
    }

    .table th, .table td {
        border: 1px solid #ffffff;
    }

    .gy-navtent2-input label {
        width: 120px;
        text-align: right;
    }

    .gy-navtent2-input {
        margin-bottom: 10px;
    }

</style>

<script type="text/javascript">
    $(document).ready(function () {
        $('#table_content > tr:odd').css("background-color", "#f5f5f5");
    });
</script>
    <form class="form-horizontal" id="search_form">
        <div class="control-group">
            <label class="control-label w-label">储位:</label>
            <div class="controls">
                <div class="input-append">
                    <input class="span2" style="width:205px;"  type="text" name="storage_position" 
                        onkeyup="if(this.value.length>8 ){alert('数字超过了8位');
                        this.value=this.value.substr(0,8);};this.value=this.value.replace(/\D/g,'')"
                        onafterpaste="if(this.value.length>8 ){alert('数字超过了8位');
                        this.value=this.value.substr(0,8);};
                        this.value=this.value.replace(/\D/g,'')" value="<?php echo $get['storage_position']?>" placeholder="请输入储位，支持模糊搜索">
                </div>
            </div>
        </div>
        
        <div class="control-group">
            <label class="control-label">截止日期:</label>
            <div class="controls">
                <input type="text" name='end' id="end"
                    value="<?php if (isset($get['end'])) {echo $get['end'];} ?>" /> 23:59:59
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">SKU：</label>
            <div class="controls">
            <input type="text" name='sku' id='sku' value="<?php echo isset($get['sku'])?$get['sku']:''; ?>"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label w-label">仓库:</label>
            <div class="controls">
                <select name='warehouseid' id="warehouseid">
                <?php 
                    $store_list = Kohana::$config->load('vmi_warehouse')->as_array();
                    ksort($store_list);
                    if (is_array($store_list) && !empty($store_list)) {
                        foreach ($store_list as $storeId => $store) {
                ?>
                            <option value="<?php echo $storeId; ?>" <?php if (isset($get['warehouseid']) && 
                                $get['warehouseid'] == $storeId) {echo "selected='selected'";} ?>>
                                <?php echo $store; ?>
                            </option>
                <?php }} ?>
                </select>
            </div>
        </div>
<!--         <div class="control-group">
            <label class="control-label">主体:</label>
            <div class="controls">
                <select name="enterprise_dominant" id="enterprise_dominant">
                    <option value="">全部</option>
                <?php $body_arr = Kohana::$config->load('enterprise_dominant')->as_array();?>
                <?php foreach ($body_arr as $k=>$v){?>
                    <option value="<?php echo $k;?>" 
                        <?php if(isset($get['enterprise_dominant']) && $k==$get['enterprise_dominant']) 
                            echo 'selected';?>>
                        <?php echo $v;?>
                    </option>
                <?php }?>
                </select>
            </div>
        </div>
    -->
        <div class="controls">
            <input type="submit" class="btn btn-info" value="查询"/>
            <a class="xiazai btn btn-default" id='download'">导出</a>
            <button class="btn btn-default" data-toggle="modal" data-target="#upform">库存盘点批量导入</button>
        </div>
     </form>


<table class="table table1 table-bordered" style="border-collapse: collapse;">
    <thead>
    <tr>
        <th>序号</th>
        <th>SKU</th>
        <th>中文名称</th>
        <!--<th>供应商</th>-->
        <th>仓库</th>
        <!--<th>主体</th>-->
        <th>储位</th>
        <th>实际库存量</th>
        <th>借料备注量</th>
        <th>销售状态</th>
    </tr>
    </thead>
    <tbody id='table_content'>
    <?php
    if(isset($lists)){
        foreach($lists as $k=>$list):?>
            <tr>
                <td><?php echo $k+1;?></td>
                <td><?php echo $list['sku']; ?></td>
                <td><?php echo $list['name']; ?></td>
                <!--<td><?php echo $list['supplier'];?></td>-->
                <td><?php echo $store_list[$list['warehouseid']];?></td>
                <!--<td><?php echo $list['enterprise_dominant'];?></td>-->
                <td><?php echo $list['storage']; ?></td>
                <td><?php echo $list['stock']; ?></td>
                <td><?php echo $list['lend']; ?></td>
                <td><?php echo $list['status']; ?></td>
            </tr>
        <?php endforeach;
    } ?>
    <tr>
        <td colspan="13">
            <div><?php if(isset($page)){echo $page;} ?>
            </div>
        </td>
    </tr>
    <tr>
        <td colspan="10" style="text-align: center">
            <div>总计<?php if(isset($num)){echo $num.'个';} ?>
            </div>
        </td>
    </tr>
    </tbody>
</table>
<div id="pager_links"></div>
<!--/** 文件上传**/-->

<div id="upform" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">库存盘点批量上传</h3>
    </div>
    <div class="modal-body">
        <form class="upSuppForm" method='post' action='<?php echo URL::site() ?>checkstore/upload' enctype="multipart/form-data" id="upload_form">
            <table class="table table-striped">
                <tr>
                    <td class="need"></td>
                    <td>批量上传</td>
                    <td> <input type='file'   style="line-height: 0px;" name='xls' class="inputxt" datatype="/.+\.xls/"  nullmsg="请选择上传文件"  errormsg="文件格式只能为xls"/></td>
                    <td><div class="Validform_checktip"></div></td>
                </tr>
                <tr>
                    <td class="need"></td>
                    <td></td>
                    <td colspan="2"></td>
                </tr>
            </table>
            <input type='submit' value='上传' class='btn btn-success'  id="upload"/>
        </form>
    </div>
<!--    <div class="modal-footer">-->
<!--        <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>-->
<!--        <button class="btn btn-primary">Save changes</button>-->
<!--    </div>-->
</div>

<script type="text/javascript">
    $(document).ready(function () {
        laydate.render({
            elem: '#end',
        });
        //上传表格
        $(".upSuppForm").Validform({
            tiptype: 2,
            postonce: true
        });

    });
    //储位筛选匹配
    function checkStor(s) {
        s = $(s);
        var c = s.val();
        s.val(c.replace(/[^A-z0-9]/g,''));
        if(c.length==4) {
            if(/^[A-z]{4}$/.test(c)) {
                alert('不能全部为字母');
                s.val(c.substr(0,3));
                return;
            }
            if(/^[A-z]{0,3}[0-9]{1,4}$/.test(c)) {

            } else {
                alert('不规则储位');
                s.val("");
            }
            return;
        }

        if(c.length>4 ) {
            alert('数字超过了4位');
            s.val(c.substr(0,4));
        }
    }

    function regStor(stor1, stor2) {
        var reg = /^[0-9]{1,4}$/;
        var reg2 = /^[A-z]{1,3}[0-9]{1,3}$/;
        if(stor1 || stor2) {
            if(stor1 && stor2) {
                if(reg.test(stor1) && reg.test(stor2)) {

                } else if(reg2.test(stor1) && reg2.test(stor2)) {
                    //判断包含字母是否相同
                    var pre1 = stor1.replace(/[^A-z]/g,'');
                    var pre2 = stor2.replace(/[^A-z]/g,'');
                    if(pre1 == pre2) {
                        return pre1;
                    } else {
                        alert('请输入相同字母开头的储位');
                        return false;
                    }
                } else {
                    alert('不允许的操作');
                    return false;
                }
            }
        }
        return true;
    }

    $("#search_form").submit(function(){
        var storage_position = $.trim($("input[name='storage_position']").val());
        var stor1 = $.trim($("input[name='stor1']").val());
        var stor2 = $.trim($("input[name='stor2']").val());

        var pre = regStor(stor1, stor2)

        if(pre) {
            if(typeof pre === "boolean") {
                pre = '';
            }

            var stor = $("input[name='stor']").val();
            var sku = $("input[name='sku']").val();
            var end = $("input[name='end']").val();
            var warehouseid = $('#warehouseid').val();
            //var enterprise_dominant = $('#enterprise_dominant').val();
            var uri = 'sku=' + sku + '&end=' + end  + '&stor=' + stor+ '&warehouseid=' + warehouseid+'&storage_position='+storage_position ; 
                //'&enterprise_dominant=' + enterprise_dominant;
            if(pre != '') {
                uri += '&pre=' + pre;
                stor1 = stor1.replace(pre, '');
                stor2 = stor2.replace(pre, '');
            }
            uri += '&stor1=' + stor1 + '&stor2=' + stor2;

            window.location.href = "<?php echo URL::base()?>checkstore/index?" + uri;
        }
        return false;
    });

    $("#download").click(function () {
        var stor1 = $.trim($("input[name='stor1']").val());
        var stor2 = $.trim($("input[name='stor2']").val());
        var warehouseid = $.trim($("#warehouseid").val());
        var pre = regStor(stor1, stor2)
        if(pre) {
            if(typeof pre === "boolean") {
                pre = '';
            }

            var stor = $("input[name='stor']").val();
            var sku = $("input[name='sku']").val();
            var end = $("input[name='end']").val();
            var uri = 'sku=' + sku + '&end=' + end  + '&stor=' + stor;
            if(pre != '') {
                uri += '&pre=' + pre;
                stor1 = stor1.replace(pre, '');
                stor2 = stor2.replace(pre, '');
            }
            uri += '&stor1=' + stor1 + '&stor2=' + stor2 + '&warehouseid=' + warehouseid;

            window.location.href = "<?php echo URL::base()?>checkstore/index?download=1&" + uri;
        }
    });

    function clearNoNum(obj) {
        obj.value = obj.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
        obj.value = obj.value.replace(/^\./g, "");  //验证第一个字符是数字而不是.
        obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    }

    function reset_() {
        $("input[name='stor1']").val("");
        $("input[name='stor2']").val("");
        $("input[name='stor']").val("");
    }

    $(function () {
        //上传表格
        $(".upSuppForm").Validform({
            tiptype: 2,
            postonce: true
        });
    });
</script>